#include "cv.h"
#include <highgui.h>
#include <stdio.h>
int matrix[1000][1280];
int main(int argc,char **argv){
	IplImage *in=cvLoadImage(argv[1],0),
		 *out;
	int x,y;
	int stat=0;
	int start_line,end_line;
	int w=in->width,h=in->height;
	int count=1;

	unsigned char *ptr;
	for(y=0;y<h;y++){
		ptr=(unsigned char *)(in->imageData+in->widthStep*y);
		for(x=0;x<w;x++)
			matrix[y][x]=ptr[x];
	}

	for(x=0;x<w;x++){
		if(count>8)	break;
		int haveblack=0;
		for(y=0;y<h;y++)
			if(matrix[y][x]<128){
				haveblack=1;
				break;
			}
		if(stat==0 && haveblack)	start_line=x;
		if(stat==1 && !haveblack){
			end_line=x;
			printf("start_line=%d\tend_line=%d\n",start_line,end_line);
			char filename[50];
			sprintf(filename,"%s_%d.jpg","area_second",count++);
			printf("%s is saved.\n",filename);
			cvSetImageROI(in,cvRect(start_line,0,end_line-start_line,h));
			out=cvCreateImage(cvSize(end_line-start_line,h),in->depth,in->nChannels);
			cvCopy(in,out,0);
			cvSaveImage(filename,out,0);
			cvResetImageROI(in);
		}
		stat=(haveblack)?1:0;
	}
	cvReleaseImage(&in);
	cvReleaseImage(&out);
	return 0;
}
